package com.example.service;

import cn.hutool.core.convert.Convert;
import com.example.entity.ZyzActivityType;
import com.example.exception.CustomException;
import com.example.mapper.ZyzActivityTypeMapper;
import com.example.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 活动类型
 */
@Service
public class ZyzActivityTypeService {
    @Autowired
    private ZyzActivityTypeMapper zyzActivityTypeMapper;

    /**
     * 添加
     * @param zyzActivityType
     */
    public void add(ZyzActivityType zyzActivityType) {
        List<ZyzActivityType> zyzActivityTypes = zyzActivityTypeMapper.selectAll(zyzActivityType);
        if (zyzActivityTypes.size() > 0){
            throw new CustomException("500","活动类型已存在");
        }
        //获取当前登录的用户
        String username = TokenUtils.getCurrentUser().getUsername();
        zyzActivityType.setCreateBy(username);
        zyzActivityType.setUpdateBy(username);
        Date date = new Date();
        zyzActivityType.setCreateTime(date);
        zyzActivityType.setUpdateTime(date);
        zyzActivityTypeMapper.insert(zyzActivityType);
    }

    /**
     * 批量删除
     * @param ids
     */
    public void deleteBatch(String ids) {
        zyzActivityTypeMapper.deleteBatch(Convert.toStrArray(ids));

    }

    /**
     * 修改
     * @param zyzActivityType
     */
    public void updateById(ZyzActivityType zyzActivityType) {
        String username = TokenUtils.getCurrentUser().getUsername();
        zyzActivityType.setUpdateBy(username);
        zyzActivityType.setUpdateTime(new Date());
        zyzActivityTypeMapper.updateById(zyzActivityType);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    public ZyzActivityType selectById(Integer id) {
        return zyzActivityTypeMapper.selectById(id);
    }

    /**
     * 查询所有
     * @param zyzActivityType
     * @return
     */
    public List<ZyzActivityType> selectAll(ZyzActivityType zyzActivityType) {
        return zyzActivityTypeMapper.selectAll(zyzActivityType);
    }

    public PageInfo<ZyzActivityType> selectPage(ZyzActivityType zyzActivityType, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<ZyzActivityType> list = zyzActivityTypeMapper.selectAll(zyzActivityType);
        return PageInfo.of(list);
    }
}
